package api

import (
	"github.com/gin-gonic/gin"
	"server/internal/app/model"
	"server/internal/app/serv"
	"server/internal/database"
	"server/internal/pkg/response"
	"time"
)

// EnterpriseUpdateApi 企业管理 修改审核
var EnterpriseUpdateApi = new(enterpriseUpdateApi)

type enterpriseUpdateApi struct {
	model.EnterpriseUpdate
}

// List 列表
func (a enterpriseUpdateApi) List(c *gin.Context) {
	Db := database.DB         //初始化Db
	Db = Common.Query(Db, c)  //查询构造
	Db = Common.Paging(Db, c) //分页

	//快速搜索
	quickSearch := c.Query("quickSearch")
	if quickSearch != "" {
		Db = Db.Where("unit_name like ?", "%"+quickSearch+"%")
	}

	_, list, total := serv.EnterpriseUpdate.Reads(Db)

	response.SuccessList(c, list, total)
}

// Add 添加
func (a enterpriseUpdateApi) Add(c *gin.Context) {

	if err := c.ShouldBindJSON(&a); err != nil {
		c.JSON(200, gin.H{"code": 1, "msg": "参数错误", "data": "1", "err": err.Error()})
		return
	}
	userInfo, _ := c.Get("userInfo")
	a.UserId = userInfo.(model.User).ID
	a.TissueId = userInfo.(model.User).TissueId
	t := time.Now()
	a.CreateTime = int(t.Unix())
	a.UpdateTime = int(t.Unix())
	a.Status = 0
	if err := serv.EnterpriseUpdate.Create(a.EnterpriseUpdate); err.Error == nil {
		response.Success(c, "", "添加成功")
	} else {
		response.Fail(c, err.Error, "添加失败")
	}
}

// Detail 单条
func (a enterpriseUpdateApi) Detail(c *gin.Context) {
	id := c.Query("id")
	_, detail := serv.EnterpriseUpdate.Read(id)
	_, detail2 := serv.Enterprise.Read(detail.EnterpriseId)
	data := make(map[string]any)
	data["yuan"] = detail2
	data["xin"] = detail
	response.SuccessData(c, data, "")
}

// Edit 编辑
func (a enterpriseUpdateApi) Edit(c *gin.Context) {

	if err := c.ShouldBindJSON(&a); err != nil {
		c.JSON(200, gin.H{"code": 1, "msg": "参数错误", "data": "1", "err": err.Error()})
		return
	}

	if err := serv.EnterpriseUpdate.Update(a.EnterpriseUpdate); err.Error == nil {
		response.Success(c, "", "编辑成功")
	} else {
		response.Fail(c, err.Error, "编辑失败")
	}
}

// Del 删除
func (a enterpriseUpdateApi) Del(c *gin.Context) {
	type Param struct {
		Ids []int `json:"ids"`
	}
	var param Param
	if err := c.ShouldBindJSON(&param); err != nil {
		c.JSON(200, gin.H{"code": 1, "msg": "参数错误", "data": "1", "err": err.Error()})
		return
	}

	if err := serv.EnterpriseUpdate.Delete(param.Ids); err.Error == nil {
		response.Success(c, "", "删除成功")
	} else {
		response.Fail(c, err.Error, "删除失败")
	}
}

// Passing 审核通过
func (a enterpriseUpdateApi) Passing(c *gin.Context) {

	if err := c.ShouldBindJSON(&a); err != nil {
		c.JSON(200, gin.H{"code": 1, "msg": "参数错误", "data": "1", "err": err.Error()})
		return
	}
	err := serv.EnterpriseUpdate.Passing(a.ID)
	if err == nil {
		response.Success(c, "", "审核成功")
	} else {
		response.Fail(c, err, "审核失败")
	}
}

// Reject 审核驳回
func (a enterpriseUpdateApi) Reject(c *gin.Context) {

	if err := c.ShouldBindJSON(&a); err != nil {
		c.JSON(200, gin.H{"code": 1, "msg": "参数错误", "data": "1", "err": err.Error()})
		return
	}
	err := serv.EnterpriseUpdate.Reject(a.ID)
	if err == nil {
		response.Success(c, "", "审核成功")
	} else {
		response.Fail(c, err, "审核失败")
	}
}
